//
//  MeProfileViewController.m
//  XPhoto
//
//  Created by sentinel on 2020/4/8.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "MeProfileViewController.h"
#import "BKActionSheet.h"
#import "UIViewController+camera.h"
#import "PhotoLibViewController.h"
#import "PhotoCameraViewController.h"
#import "BKNavigationController.h"
#import "ProfileEditViewController.h"
#import "SettingViewController.h"
#import "CameraViewController.h"
#import "BKSoloNavigationController.h"

#define ACTION_CHANGE_AVATAR      0
#define ACTION_CHANGE_COVER      1

@interface MeProfileViewController ()<BKActionSheetDelegate,PhotoLibViewDelegate, PhotoEditDelegate>
@property(nonatomic, assign) NSInteger action;
@end

@implementation MeProfileViewController

- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([UserProfileViewController class]) bundle:nil]){
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeCover:)];
    [self.coverImageView addGestureRecognizer:tap];
    self.settingButton.hidden = NO;
    self.editButton.hidden = NO;
}


- (IBAction)editProfile:(id)sender{
    ProfileEditViewController *vc = [[ProfileEditViewController alloc] init];
    BKNavigationController *nav = [[BKNavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}
- (IBAction)clickSettingButton:(id)sender{
    SettingViewController *vc = [[SettingViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)takePhoto{
    CameraViewController *vc = [[CameraViewController alloc] init];
    vc.delegate = self;
    vc.maxVideoCount = 0;
    vc.maxSelectedCount = 1;
    vc.supportModes = @[@CAMERA_MODE_PHOTO_LIB, @CAMERA_MODE_TAKE_PHOTO];
    
    BKSoloNavigationController *nav = [[BKSoloNavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}
- (IBAction)changeAvatar:(id)sender{
    self.action = ACTION_CHANGE_AVATAR;
    [self takePhoto];
}
- (IBAction)changeCover:(id)sender{
    self.action = ACTION_CHANGE_COVER;
    [self takePhoto];
}

- (void)photoLibViewController:(id)photoLibViewController didSelectPhotos:(NSArray<PhotoAsset *> *)photos{
    if (!photos || photos.count == 0) {
        return;
    }
    PhotoAsset *photoAsset = photos.firstObject;
    if (self.action == ACTION_CHANGE_AVATAR) {
        [self uploadAvatar:photoAsset];
    }
    if (self.action == ACTION_CHANGE_COVER) {
        [self uploadCover:photoAsset];
    }
    
}
- (void)uploadAvatar:(PhotoAsset *)photoAsset{
    
    [BKIndicator showTips:NSLocalizedString(@"uploading", @"提交数据")
                   inView:self.view
                 duration:0
               completion:nil];
     [User updateAvatar:photoAsset
    photoUploadProgress:^(NSString * _Nonnull key, float p) {
         
     }
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
         if (error) {
             [BKIndicator showTips:error.localizedDescription
                            inView:self.view
                          duration:1.0f
                        completion:nil];
             return;
         }
         [BKIndicator showTips:NSLocalizedString(@"submit_success", @"提交数据")
                        inView:self.view
                      duration:1.0f
                    completion:nil];
         [self.avatarButton setImage:[UIImage imageWithContentsOfFile:photoAsset.originPath] forState:UIControlStateNormal];
     }];
}
- (void)uploadCover:(PhotoAsset *)photoAsset{
    [BKIndicator showTips:NSLocalizedString(@"uploading", @"提交数据")
                   inView:self.view
                 duration:0
               completion:nil];
      [User updateCover:photoAsset
    photoUploadProgress:^(NSString * _Nonnull key, float p) {
          
      }
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
          if (error) {
              [BKIndicator showTips:error.localizedDescription
                             inView:self.view
                           duration:1.0f
                         completion:nil];
              return;
          }
          [BKIndicator showTips:NSLocalizedString(@"submit_success", @"提交数据")
                         inView:self.view
                       duration:1.0f
                     completion:nil];
          [self.coverImageView setImage:[UIImage imageWithContentsOfFile:photoAsset.originPath]];
      }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
